Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Inherit from Error without performance penalties, instanceof behaves as expected and the error stack is correct
Inherit from Error without performance penalties, instanceof
behaves as expected and the error stack is correct. This library can be used cross-browser using Browserify.
A lot of solutions for inheriting from Error
in javascript are error-prone (pun intended) and tricky.
Using this library you can inherit Error
just like you would inherit from any other javascript object/class.
Here is an example using the inherits library.
var Throwable = require('throwable');
function MyError(wrapped)
{
Throwable.call(this, wrapped);
this.name = 'MyError';
}
require('inherits')(MyError, Throwable);
And this is how you can throw your error:
function failure()
{
throw new MyError(Error('Something went wrong!'));
}
This library has a few advantage some other solutions might not have:
Using instanceof
wil work as expected:
try
{
failure();
}
catch(err)
{
console.log(err instanceof Error); // true
console.log(err instanceof Throwable); // true
console.log(err instanceof MyError); // true
}
And the stack
, fileName
(firefox), lineNumber
(firefox) attributes will be set correctly (instead of pointing to a line in the Throwable constructor).
Also, the stack
attribute is accessed only when you need it, Error.captureStackTrace
is not used either. Accessing any of these two is a big performance hit in Node.js and Chrome.
FAQs
Inherit from Error without performance penalties, instanceof behaves as expected and the error stack is correct
The npm package throwable receives a total of 1 weekly downloads. As such, throwable popularity was classified as not popular.
We found that throwable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.